<?

  error_reporting(E_ALL);
  ini_set('display_errors', 1);

if (empty($_COOKIE['user'])) {
  header('Location: index.php');
}
if (empty($_GET['id'])) {
  header('Location: dashboard.php');
}

include_once 'controller.inc.php';
$db_link = db_connect();
$cur_user = db_getUserByEmail($_COOKIE['user']);
$event = db_getEvent($_GET['id']);
$event_roles = db_getRolesForEvent($event);
db_disconnect($db_link);

// group roles by name
$role_list = array();
foreach ($event_roles as $role) {
  $role_id = $role->getID();
  $role_name = $role->getName();
  $role_user = $role->getUser();
  $add_role = TRUE;
  
  foreach ($role_list as $list_role_name => $list_role_users) {
    if ($role_name == $list_role_name) {
      $list_role_users[$role_id] = $role_user;
      $role_list[$list_role_name] = $list_role_users;
      $add_role = FALSE;
    }
  }
  
  if ($add_role) {
    $role_list[$role_name] = array($role_id => $role_user);
  }
}

include("header.inc.php");

?>

<?
if (isset($_GET['success'])) {
  $success_info = $_GET['success'];
  $success_info = explode('_', $success_info);
  echo '<p class="success">'.$success_info[0].' '.$success_info[1].' successfully.</p>';
}
?>

<h1>Event - <? echo $event->getName() ?></h1>

<form name="join_event" action="join_event.php" method="post">
<input type="hidden" name="unjoin_role" id="unjoin_role_id" value="0">

<? echo str_replace(array("\r\n", "\n", "\r"), "<br/>", $event->getDescription()) ?><br/><br/>

<strong>Location:</strong><br/>
<? echo $event->getLocation() ?><br/><br/>

<strong>Start Time:</strong><br/>
<? echo $event->getStartTime()->format('l, F j (g:i a)') ?><br/><br/>

<strong>End Time:</strong><br/>
<? echo $event->getEndTime()->format('l, F j (g:i a)') ?><br/><br/>

<strong>Event Manager:</strong><br/>
<? echo $event->getOwner()->getName() ?><br/><br/>

<strong>Participants:</strong><br/>
<ul>
<?

if (empty($role_list)) {
  echo '  <li>There are no participants in this event yet.</li>';
}

$open_roles = 0;

foreach ($role_list as $role_name => $role_users) {
  if (count($role_users) == 1) {
    foreach ($role_users as $role_id => $user) {
      if (empty($user)) {
        echo '  <li class="list_button"><input type="radio" name="join_role" value="'.$role_id.'"> '.
             $role_name.' (not assigned)</li>';
        $open_roles += 1;
      }
      else {
        echo '  <li>'.$role_name.': <a href="view_user.php?id='.$user->getID().'">'.$user->getName().'</a>';
        if ($user->getID() == $cur_user->getID()) {
          echo '  <input class="button_small" type="submit" name="action" value="Cancel" onclick="set_unjoin_event('.$role_id.')">';
        }
        echo '</li>';
      }
    }
  }
  
  else {
    echo '  <li>'.$role_name.':<ul>';
    foreach ($role_users as $role_id => $user) {
      if (empty($user)) {
        echo '  <li class="list_button"><input type="radio" name="join_role" value="'.$role_id.'"> (open position)</li>';
        $open_roles += 1;
      }
      else {
        echo '  <li><a href="view_user.php?id='.$user->getID().'">'.$user->getName().'</a>';
        if ($user->getID() == $cur_user->getID()) {
          echo '  <input class="button_small" type="submit" name="action" value="Cancel" onclick="set_unjoin_event('.$role_id.')">';
        }
        echo '</li>';
      }
    }
    echo '</ul></li>';
  }
}

?>
</ul>

<p>
<?
if ($open_roles > 0) {
  echo '<input type="submit" name="action" value="Join Event"> - ';
}
if ($event->getOwnerID() == $cur_user->getID()) {
  echo '<a href="edit_event.php?id='.$event->getID().'">Edit Event</a> - ';
}

?>
  <a href="dashboard.php">Back</a>
</p>

</form>
  
<?

if ($event->getOwnerID() == $cur_user->getID()) {
  echo '<form name="delete_event" action="delete_event.php" method="post" onsubmit="return confirm_deletion()">';
  echo '  <input type="hidden" name="id" value="'.$event->getID().'">';
  echo '  <p><input type="submit" name="action" value="Delete Event"></p>';
  echo '</form>';
}

?>

<script type="text/javascript">
function confirm_deletion() {
  var confirmed = confirm('Are you sure you would like to delete this event?');
  if (!confirmed) {
    return false;
  }
}

function set_unjoin_event(new_id) {
  document.getElementById('unjoin_role_id').setAttribute('value', new_id);
}
</script>

<?

include("footer.inc.php");

?>
